package com.tengju.user.infrastructure.repository.club.convert;

import com.tengju.user.domain.model.club.ClubId;
import com.tengju.user.domain.model.club.Club;
import com.tengju.user.domain.model.club.ClubZoneRelation;
import com.tengju.user.domain.model.club.ZoneId;
import com.tengju.user.domain.model.user.UserId;
import com.tengju.user.domain.model.user.UserIdCode;
import com.tengju.user.domain.shared.DateUtil;
import com.tengju.user.infrastructure.persistence.mybatis.dataobject.ClubMetaDO;
import com.tengju.user.infrastructure.persistence.mybatis.dataobject.ClubZoneRelationDO;

/**
 * @author chenhang
 * @Date 2021/1/13
 */
public class ClubConvert {

    public static Club clubMetaToModel(ClubMetaDO clubMetaDO) {
        if (clubMetaDO == null) {
            return null;
        }
        return new Club(
                new ClubId(clubMetaDO.getId()),
                UserIdCode.of(clubMetaDO.getFounderIdCode()),
                clubMetaDO.getFounderUserId() == 0L ? null : new UserId(clubMetaDO.getFounderUserId()),
                clubMetaDO.getName(),
                clubMetaDO.getHeadImg(),
                clubMetaDO.getCreateBy(),
                clubMetaDO.getUpdateBy(),
                DateUtil.dateToLocalDateTime(clubMetaDO.getUpdateTime())
        );
    }

    public static ClubMetaDO metaToDO(Club meta) {
        ClubMetaDO clubMetaDO = new ClubMetaDO();
        if (meta.getClubId() != null) {
            clubMetaDO.setId(meta.getClubId().getValue());
        }
        clubMetaDO.setFounderIdCode(meta.getFounderIdCode() == null ? 0 : meta.getFounderIdCode().getValue());
        clubMetaDO.setFounderUserId(meta.getFounderUserId() == null ? 0 : meta.getFounderUserId().getValue());
        clubMetaDO.setName(meta.getClubName());
        clubMetaDO.setHeadImg(meta.getClubHeadImg());
        clubMetaDO.setCreateBy(meta.getCreateBy());
        clubMetaDO.setUpdateBy(meta.getUpdateBy());
        return clubMetaDO;
    }

    public static ClubZoneRelation relationToModel(ClubZoneRelationDO clubZoneRelationDO) {
        if (clubZoneRelationDO == null) {
            return null;
        }
        return new ClubZoneRelation(
                new ClubId(clubZoneRelationDO.getClubId()),
                new ZoneId(clubZoneRelationDO.getZoneId()),
                clubZoneRelationDO.getCreateBy(),
                clubZoneRelationDO.getUpdateBy()
        );
    }

    public static ClubZoneRelationDO modelToRelation(ClubZoneRelation clubZoneRelation) {
        if (clubZoneRelation == null) {
            return null;
        }
        ClubZoneRelationDO clubZoneRelationDO = new ClubZoneRelationDO();
        clubZoneRelationDO.setClubId(clubZoneRelation.getClubId().getValue());
        clubZoneRelationDO.setZoneId(clubZoneRelation.getZoneId().getValue());
        clubZoneRelationDO.setCreateBy(clubZoneRelation.getCreateBy());
        clubZoneRelationDO.setUpdateBy(clubZoneRelation.getCreateBy());
        return clubZoneRelationDO;
    }
}
